/*
 * frmActivida.java
 *
 * Created on 8 de mayo de 2008, 10:53
 */

package GUI;
import javax.swing.*;
import ModelosDatos.*;
import AccesoDatos.*;
import ReglasNegocio.*;
import java.util.Date;
import org.jdesktop.swingx.autocomplete.AutoCompleteDecorator;
import javax.swing.table.*;
/**
 *
 * @author  Hugol
 */
public class frmActividad extends javax.swing.JFrame {
    
    /** Creates new form frmActivida */
    public frmActividad() {
        initComponents();
        this.setLocationRelativeTo(null);
        this.comboFincas.setModel(this.modeloFincas());
        AutoCompleteDecorator.decorate(comboFincas);
        this.tablaActividades.setModel(this.modeloLogActividades());
        this.comboActividades.setModel(this.modeloActividades());
        AutoCompleteDecorator.decorate(comboActividades);
    }
    
    private void MostrarMenu(java.awt.event.MouseEvent e) {
        if (this.PopupMenu.isPopupTrigger(e))
            this.PopupMenu.show(e.getComponent(), e.getX(), e.getY());
    }
    
    private DefaultComboBoxModel modeloActividades() {
        DefaultComboBoxModel modelo=new DefaultComboBoxModel();
        String buscar="select * from actividad";
        ConMySQL con=new ConMySQL();
        con.Conectar();
        Vista v=con.crearVista(buscar);
        for(int i=0;i<v.getNRegistros();i++) {
            long idActividad=(Long)v.getRegistro(i, 0);
            Dato id=new Dato("id",String.valueOf(idActividad));
            Actividad a=new Actividad(id);
            modelo.addElement(a);
        }
        return modelo;
    }
    
    private ModeloLogActividad modeloLogActividades() {
        Finca f=(Finca)this.comboFincas.getSelectedItem();
        ModeloLogActividad modelo=new ModeloLogActividad();
        
        modelo.addColumn("Id");
        modelo.addColumn("Actividad");        
        modelo.addColumn("Fecha");
        modelo.addColumn("Area Cubierta (mz)");
        modelo.addColumn("# Personas");
        
        
        String buscar="select * from log_actividad where fkfinca='"+f.getId()+"'";
        ConMySQL con=new ConMySQL();
        con.Conectar();
        Vista v=con.crearVista(buscar);
        if(v.getNRegistros()>0) {
            for(int i=0;i<v.getNRegistros();i++) {
                long idLog=(Long)v.getRegistro(i, 0);
                Dato id=new Dato("id",String.valueOf(idLog));
                logActividad la=new logActividad(id);
                String[] fila={la.getId(),la.getFkactividad().getNombre(),la.getFecha().toString(),String.valueOf(la.getAreacubierta()),String.valueOf(la.getNumpersonas())};
                modelo.addRow(fila);
            }
        }
        return modelo;
    }
    private DefaultComboBoxModel modeloFincas() {
        DefaultComboBoxModel modelo=new DefaultComboBoxModel();
        String buscar="select * from finca";
        ConMySQL con=new ConMySQL();
        con.Conectar();
        Vista v=con.crearVista(buscar);
        if (v.getNRegistros()>0) {
            for(int i=0;i<v.getNRegistros();i++) {
                long id=(Long)v.getRegistro(i, 0);
                Dato idFinca=new Dato("id",String.valueOf(id));
                Finca f=new Finca(idFinca);
                modelo.addElement(f);
            }
        }
        
        return modelo;
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        PopupMenu = new javax.swing.JPopupMenu();
        ItemNuevo = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JSeparator();
        ItemEliminar = new javax.swing.JMenuItem();
        jPanel3 = new javax.swing.JPanel();
        jLabel7 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        comboFincas = new javax.swing.JComboBox();
        jScrollPane3 = new javax.swing.JScrollPane();
        tablaActividades = new javax.swing.JTable();
        jButton5 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        comboActividades = new javax.swing.JComboBox();

        ItemNuevo.setText("Nueva Actividad");
        PopupMenu.add(ItemNuevo);
        PopupMenu.add(jSeparator1);

        ItemEliminar.setText("Eliminar");
        PopupMenu.add(ItemEliminar);

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Gestion de Actividades"));

        jLabel7.setFont(new java.awt.Font("Tahoma", 0, 24));
        jLabel7.setText("GESTION DE ACTIVIDADES");

        jLabel9.setText("Finca");

        comboFincas.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comboFincasActionPerformed(evt);
            }
        });

        tablaActividades.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        tablaActividades.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                tablaActividadesMouseReleased(evt);
            }
        });
        jScrollPane3.setViewportView(tablaActividades);

        jButton5.setMnemonic('A');
        jButton5.setText("Asignar");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        jLabel1.setText("Actividades");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 473, Short.MAX_VALUE)
                            .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel3Layout.createSequentialGroup()
                                    .addComponent(jLabel1)
                                    .addGap(23, 23, 23)
                                    .addComponent(comboActividades, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel3Layout.createSequentialGroup()
                                    .addComponent(jLabel9)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(comboFincas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addComponent(jButton5))
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel7)
                        .addGap(94, 94, 94))))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel7)
                .addGap(11, 11, 11)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel9)
                    .addComponent(comboFincas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel1)
                    .addComponent(comboActividades, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jButton5)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 196, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(43, 43, 43)
                .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void comboFincasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboFincasActionPerformed
// TODO add your handling code here:
    this.tablaActividades.setModel(this.modeloLogActividades());
}//GEN-LAST:event_comboFincasActionPerformed

private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
// TODO add your handling code here:

    Finca f=(Finca)this.comboFincas.getSelectedItem();
    Actividad a=(Actividad)this.comboActividades.getSelectedItem();
    
    logActividad log=new logActividad();
    log.setFkfinca(f);
    log.setFkactividad(a);
    log.setAreacubierta(0);
    log.setFecha(new Date());
    log.setNumpersonas(0);
    log.Crear();
    this.tablaActividades.setModel(this.modeloLogActividades());
}//GEN-LAST:event_jButton5ActionPerformed

private void tablaActividadesMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tablaActividadesMouseReleased
    // TODO add your handling code here:
    this.MostrarMenu(evt);
}//GEN-LAST:event_tablaActividadesMouseReleased
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new frmActividad().setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem ItemEliminar;
    private javax.swing.JMenuItem ItemNuevo;
    private javax.swing.JPopupMenu PopupMenu;
    private javax.swing.JComboBox comboActividades;
    private javax.swing.JComboBox comboFincas;
    private javax.swing.JButton jButton5;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTable tablaActividades;
    // End of variables declaration//GEN-END:variables
    
}
